//
//  AppDelegate.h
//  Address-book📞
//
//  Created by Mars_ZXB on 16/8/6.
//  Copyright © 2016年 BAT3. All rights reserved.
//


#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

#import "VC2ndOfContact.h"
#import "ModelOfContact.h"
#import "VC1stOfContact.h"
#import "TextOfInfo.h"
#import "AlertOfWarn.h"
#import "ImageViewOfVC2nd.h"
@interface VC2ndOfContact () <AlertOfWarnDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, retain) ModelOfContact *model;

@property (nonatomic, retain) ImageViewOfVC2nd *picOfPerson;

@property (nonatomic, retain) TextOfInfo *textOfName;

@property (nonatomic, retain) TextOfInfo *textOfPhoneNumber;

@property (nonatomic, retain) TextOfInfo *textOfGender;

@property (nonatomic, retain) TextOfInfo *textOfAge;

@property (nonatomic, retain) UIImagePickerController *imagePicker;

@end

@implementation VC2ndOfContact
#pragma mark - 重写 dealloc

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configNavi];
    [self createSubViews];
    
    // Do any additional setup after loading the view.

}
#pragma mark - 配置 Navi
- (void)configNavi {
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"添加联系人";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:0 target:self action:@selector(handleCancel:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:0 target:self action:@selector(handleSave:)];
}
/** 取消事件 */
- (void)handleCancel:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
}
/** 保存事件 */
- (void)handleSave:(UIBarButtonItem *)button {
    // alert 弹出提醒.
    if ([self.textOfName.text isEqualToString:@""] || [self.textOfPhoneNumber.text isEqualToString:@""]) {
        AlertOfWarn *warning = [AlertOfWarn alertControllerWithTitle:@"警告" message:@"没有输入姓名或者联系方式" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:warning animated:YES completion:^{
        }];
    }
    // 赋值给 Model 数据
    else {
     self.model = [[ModelOfContact alloc]init];
     self.model.image = self.picOfPerson.image;
     self.model.name = self.textOfName.text;
     self.model.gender = self.textOfGender.text;
     self.model.age = self.textOfAge.text.integerValue;
     self.model.phoneNumber = self.textOfPhoneNumber.text;
     self.model.section = [self.textOfName.text substringToIndex:1].uppercaseString;
    // 协议传值
     if ([self.delegate respondsToSelector:@selector(addContact:)]) {
        [self.delegate addContact:self.model];
     }
    // 返回界面
     [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - AlertView协议方法.
- (void)handleClickButton:(AlertOfWarn *)alert {
    
}

#pragma mark - 创建子视图.
- (void)createSubViews {
    //头像
    self.picOfPerson = [[ImageViewOfVC2nd alloc]initWithFrame:CGRectMake(10, 100, WIDTH / 3.2, (HEIGHT - 100) / 3.5)];
    self.picOfPerson.backgroundColor = [UIColor grayColor];
    // target/action
    [self.picOfPerson addTarget:self action:@selector(pushAlbum:)];
    [self.view addSubview:self.picOfPerson];
    
    // 姓名
    self.textOfName = [[TextOfInfo alloc]initWithFrame:CGRectMake(WIDTH / 3.2 + 30, 100, WIDTH / 1.8, (HEIGHT - 100) / 3.5 / 4.5)];
    self.textOfName.placeholder = @"姓名";
    [self.view addSubview:self.textOfName];

    // 性别
    self.textOfGender = [[TextOfInfo alloc]initWithFrame:CGRectMake(WIDTH / 3.2 + 30, 100 + (HEIGHT - 100) / 3.5 / 4.5 * 1.8 , WIDTH / 1.8 / 2.f - 20, (HEIGHT - 100) / 3.5 / 4.5)];
    self.textOfGender.placeholder = @"性别";
    [self.view addSubview:self.textOfGender];
    // 年龄
    self.textOfAge = [[TextOfInfo alloc]initWithFrame:CGRectMake(WIDTH / 3.2 + 35 + WIDTH / 1.8 / 2.f, 100 + (HEIGHT - 100) / 3.5 / 4.5 * 1.8 , WIDTH / 1.8 / 2.f - 20, (HEIGHT - 100) / 3.5 / 4.5)];
    self.textOfAge.placeholder = @"年龄";
    self.textOfAge.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:self.textOfAge];
    
    // 联系方式
    self.textOfPhoneNumber = [[TextOfInfo alloc]initWithFrame:CGRectMake(WIDTH / 3.2 + 30, 100 + (HEIGHT - 100) / 3.5 / 4.5 * 3.5, WIDTH / 1.8, (HEIGHT - 100) / 3.5 / 4.5)];
    self.textOfPhoneNumber.placeholder = @"联系方式";
    self.textOfPhoneNumber.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:self.textOfPhoneNumber];

}
/** 触摸 ImageView 事件, 弹出 PickerController */
- (void)pushAlbum:(ImageViewOfVC2nd *)imageView {
    // 弹出 pickerController
    [self configPicker];
}

#pragma mark - 配置 picker
- (void)configPicker {
    if (self.imagePicker == nil) {
        self.imagePicker = [[UIImagePickerController alloc]init];
        self.imagePicker.delegate = self;
    }
    
    [self presentViewController:self.imagePicker animated:YES completion:^{
    }];
}

#pragma mark - pickerDelegate 协议方法.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    // 获取图片
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    self.picOfPerson.image = image;
    // 退出 picker 界面
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}



#pragma mark - other
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/** 点击空白 收回键盘 */
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
